package com.uphone.magicleafteacher.pro.activity.startstudent;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.mvp.BaseMvpActivity;
import com.uphone.magicleafteacher.pro.activity.StudentDetailsActivity;
import com.uphone.magicleafteacher.pro.adapter.StudentStartAdapter;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.vo.StartStudentBean;

import butterknife.BindView;
import butterknife.OnClick;

public class StartStudentActivity extends BaseMvpActivity<StartStudentPresenter> implements StartStudentContract.View {

    @BindView(R.id.img_h)
    ImageView imgH;
    @BindView(R.id.recy_student)
    RecyclerView recyStudent;
    @BindView(R.id.refreshLayout)
    protected SmartRefreshLayout refreshLayout;
    private StudentStartAdapter mAdapter;


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);

    }

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_starstudent;
    }


    @Override
    protected void initView() {
        recyStudent.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new StudentStartAdapter();
        recyStudent.setAdapter(mAdapter);
    }

    @Override
    protected void initTitle() {
    }

    @Override
    protected void initData() {
        mPresenter.postStart(1);
    }

    @Override
    protected void initListener() {
        CommonUtil.initRefresh(this, refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.postStart(1);
                refreshlayout.finishRefresh(1500);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                mPresenter.postStart(mPresenter.page + 1);
                refreshlayout.finishLoadMore(1500);
            }
        });

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            CommonUtil.startIntent(view.getContext(), StudentDetailsActivity.class, mAdapter.getItem(position).getStarId());
        });
    }


    @OnClick(R.id.img_h)
    public void onViewClicked() {
        finish();
    }

    //返回数据
    @Override
    public void updateView(StartStudentBean bean) {

        if (bean != null) {
            if (mPresenter.page == 1) {
                mAdapter.setNewData(bean.getStarStudents());
            } else {
                mAdapter.addData(bean.getStarStudents());
            }
        } else if (mPresenter.page != 1) {
            mPresenter.page = mPresenter.page - 1;
            ToastUtils.showShort("暂无更多数据");
        }
    }
}
